package com.lukebang.service.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.lukebang.dao.TerritoryDAO;
import com.lukebang.pojo.Territory;
import com.lukebang.service.ITerritoryService;
import com.lukebang.util.redis.JedisPoolUtils;

import redis.clients.jedis.Jedis;

/**
 * 
 * TerritoryServiceImpl 上午11:16:36
 * 
 * Copyright zhaocj Inc. All rights reserved. Love ME Like Justin Bieber.
 */

@Service("territorySER")
public class TerritoryServiceImpl implements ITerritoryService {
	@Resource
	TerritoryDAO territoryDAO;

	public TerritoryDAO getTerritoryDAO() {
		return territoryDAO;
	}
	
	public void setTerritoryDAO(TerritoryDAO territoryDAO) {
		this.territoryDAO=territoryDAO;
	}
	
	@Override
	public List<Territory> selectAvailableProvinceByWorker() {
		List<Territory> territoryList = null;
		Jedis jedis = JedisPoolUtils.getJedis();
		jedis.select(2);
		String provinces = jedis.get("provinces");
		if (provinces == null | "".equals(provinces)) {
			// 查询出来可以使用的省份的code
			List<String> codeByWorker = territoryDAO.selectAvailableProvinceCodeByWorker();
			// 根据code的来查询出来可使用的省份
			territoryList = territoryDAO.selectAvailableProvinceByCode(codeByWorker);
			jedis.set("provinces", JSON.toJSONString(territoryList));
			return territoryList;
		}
		territoryList = JSON.parseArray(provinces, Territory.class);
		return territoryList;
	}

	@Override
	public List<Territory> selectAvailableCityByProvince(String provinceCode) {
		List<Territory> territoryList = null;
		Jedis jedis = JedisPoolUtils.getJedis();
		jedis.select(2);
		String citys = jedis.get(provinceCode);
		if (citys == null | "".equals(citys)) {
			List<String> cityCodes = territoryDAO.selectAvailableCityCodeByProvince(provinceCode);
			territoryList=territoryDAO.selectAvailableCitesByCode(cityCodes);
			jedis.set(provinceCode, JSON.toJSONString(territoryList));
			return territoryList;
		}
		territoryList = JSON.parseArray(citys, Territory.class);
		return territoryList;
	}

	@Override
	public Territory selectTerritoryPriceByCity(String provinceCode, String cityCode) {
		
		return territoryDAO.selectTerritoryPriceByCity(provinceCode + "/" + cityCode, 1);
	}

	@Override
	public List<Territory> selectAvailableCountryByCity(String cityCode) {
		List<Territory> territoryList = null;
		Jedis jedis = JedisPoolUtils.getJedis();
		jedis.select(2);
		String countrys = jedis.get(cityCode);
		if (countrys == null | "".equals(countrys)) {
			territoryList=territoryDAO.selectAvailableCountryByCity(cityCode);
			jedis.set(cityCode, JSON.toJSONString(territoryList));
			return territoryList;
		}
		territoryList = JSON.parseArray(countrys, Territory.class);
		return territoryList;
	}

	@Override
	public List<Territory> findAllProvince(Integer level) {
		// TODO Auto-generated method stub
		return territoryDAO.selectAllProvince(level);
	}

	@Override
	public List<Territory> findAllCity(String parentId) {
		// TODO Auto-generated method stub
		return territoryDAO.selectAllCity(parentId);
	}

	@Override
	public List<Territory> findAllCountry(String code) {
		// TODO Auto-generated method stub
		return territoryDAO.findAllCountry(code);
	}

	@Transactional(readOnly =false, propagation = Propagation.REQUIRES_NEW)
	@Override
	public boolean isCovered(String name) {
		// TODO Auto-generated method stub
		Integer count=territoryDAO.selectCountByName(name);
		if (count>0) {
			return true;
		}
		return false;
	}

	//查询该所选区域所富含的师父个数
	@Override
	public Integer selectCountWorkerByName(String name) {
		// TODO Auto-generated method stub
		Integer count=territoryDAO.selectCountWorkerByName(name);
		System.err.println("count:"+count);
		return count;
	}

}
